/*-
 * Copyright (c) 2009 Jonathan Anderson
 * All rights reserved.
 *
 * WARNING: THIS IS EXPERIMENTAL SECURITY SOFTWARE THAT MUST NOT BE RELIED
 * ON IN PRODUCTION SYSTEMS.  IT WILL BREAK YOUR SOFTWARE IN NEW AND
 * UNEXPECTED WAYS.
 * 
 * This software was developed at the University of Cambridge Computer
 * Laboratory with support from a grant from Google, Inc. 
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/stat.h>

#include <err.h>
#include <signal.h>
#include <stdlib.h>
#include <sysexits.h>
#include <unistd.h>

#include <iostream>
#include <sstream>
#include <string>

#include "Server.h"

using namespace std;


//! signal handler
void handle_signal(int);

//! the actual server
Server server;


int main(int argc, char *argv[])
{
	cout << "User Angel" << endl;

	char *homedir = getenv("HOME");

	signal(SIGHUP, handle_signal);
	signal(SIGINT, handle_signal);
	signal(SIGSEGV, handle_signal);

	if(strlen(homedir) >= 80)
	{
		fprintf(stderr, "Obscenely long $HOME variable: %s", homedir);
		return 1;
	}

	char address[128];
	sprintf(address, "%s/.user-angel", homedir);

	cout << "Binding to '" << address << "'... "; cout.flush();
	if(!server.bind(address))
		err(EX_IOERR, "\nError binding to address '%s'", address);

	cout << "done." << endl;

	cout << "Running..." << endl;
	server.run();

	cout << "Shutting down... "; cout.flush();
	server.shutdown();
	cout << "done." << endl;

	return 0;
}



void handle_signal(int sig)
{
	char *error = "unknown; this should NOT happen";
	bool dumpCore = true;

	switch(sig)
	{
		case SIGHUP:    error = "SIGHUP";                       break;
		case SIGINT:    error = "SIGINT";    dumpCore = false;  break;
		case SIGSEGV:   error = "SIGSEGV";                      break;
	}

	cerr << "Caught signal " << sig << " (" << error << "); shutting down..."
	     << endl;

	server.shutdown();
	kill(getpid(), sig);

	if(dumpCore) abort();
	else exit(0);
}

